package com.longxi.gamer_base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.BuildConfig;
import com.alibaba.android.arouter.core.LogisticsCenter;
import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.jess.arms.base.BaseApplication;
import com.jess.arms.integration.AppManager;
import com.jess.arms.utils.ArmsUtils;
import com.longxi.gamer_base.utils.SharedPreferencesUtils;

import java.util.Stack;

public class MyApplication extends BaseApplication {
    private static final String TAG = "zp";
    private static  MyApplication instance;
    private static Stack<Activity> activityStack = new Stack<>();
    public static final String KEY_ACCOUNT_INFO = "KEY_ACCOUNT_INFO";

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initCloudChannel(this);
        //路由初始化
        if (BuildConfig.DEBUG) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(this);
        //Module类的APP初始化
        modulesApplicationInit();
    }

    private void modulesApplicationInit(){
        for (String moduleImpl : ModuleConfig.MODULESLIST){
            try {
                Class<?> clazz = Class.forName(moduleImpl);
                Object obj = clazz.newInstance();
                if (obj instanceof IComponentApplication){
                    ((IComponentApplication) obj).onCreate(MyApplication.getInstance());
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }
    }


    public static MyApplication getInstance() {
        if (instance == null) {
            instance = new MyApplication();
            instance.onCreate();
        }
        return instance;
    }

    //往栈中添加activity
//    public void addActivity(Activity activity) {
//        if (activity != null) {
//            activityStack.add(activity);
//        }
//    }
//
//    //从栈中移出activity
//    public void removeActivity(Activity activity) {
//        if (activity != null) {
//            new AppManager().removeActivity(activity);
////            activityStack.remove(activity);
//        }
//    }

    //依次销毁activity
    private void finishActivity() {
        new AppManager().killAll();
//        for (int i = 0; i < activityStack.size(); i++) {
//            if (activityStack.get(i) != null && !activityStack.get(i).isFinishing()) {
//                activityStack.get(i).finish();
//            }
//        }
//        activityStack.clear();
    }

    //跳转到登录界面
    public void gotoLoginActivity() {
        BaseAppConst.accountInfo=null;
        SharedPreferencesUtils.setParam(this, KEY_ACCOUNT_INFO, "");
        finishActivity();
        Postcard postcard = ARouter.getInstance().build("/activity/LoginActivity");
        ARouter.openDebug();
        LogisticsCenter.completion(postcard);
        Class<?> destination = postcard.getDestination();
        Intent intent = new Intent();
        intent.setClass(this, destination);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        ArmsUtils.makeText(getApplicationContext(),"登陆失效");
    }

    /**
     * 初始化云推送通道
     * @param applicationContext
     */
    private void initCloudChannel(Context applicationContext) {
        PushServiceFactory.init(applicationContext);
        CloudPushService pushService = PushServiceFactory.getCloudPushService();
        pushService.register(applicationContext, new CommonCallback() {
            @Override
            public void onSuccess(String response) {
                Log.i(TAG, "init cloudchannel success");
            }
            @Override
            public void onFailed(String errorCode, String errorMessage) {
                Log.i(TAG, "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
            }
        });
    }
}
